<?php
/**START GENERAL OPTIONS**/
function bs_general_feeds_providers_list()
{
	$options = get_option( 'bs-general-options' );
	$providers_count = isset($options['providers']) ? count($options['providers']) : 0;
	?>
	<div class="options_container">
		<h3 class="options_header">Feed Provider Settings</h3>
		<div class="section_instructions">
			<h4>Site Providers</h4>
		</div><!-- .section_instructions -->
		
		<div id="section_container" class="section_container">
			<?php
			$i = 0;
			for ( ; $i < $providers_count; $i++ )
			{ ?>
				<p style="margin: 0 0 30px 0;" id="provider-<?php echo $i; ?>">
					<label for="provider-name-<?php echo $i; ?>">Feed Provider:</label>
					<input type="text" id="provider-name-<?php echo $i; ?>" name="<?php echo "bs-general-options[providers][$i][name]"; ?>" value="<?php echo $options['providers'][$i]['name']; ?>" />
					
					<label for="provider-source-<?php echo $i; ?>">Source:</label>
					<input type="text" id="provider-source-<?php echo $i; ?>" name="<?php echo "bs-general-options[providers][$i][source]"; ?>" value="<?php echo $options['providers'][$i]['source']; ?>" />
					
					<a style="cursor:pointer;" id="remove-provider-<?php echo $i; ?>" class="secondary remove-provider">Remove</a>
				</p>
				<?php
			}
			?>
		</div><!-- .section_container -->
		<input style="margin-bottom:20px;" type="button" id="add-provider" class="secondary" value="Add Feed Provider" />
	</div><!-- .options_container -->
	
	<script type="text/javascript">
		var requestsFile = '<?php bloginfo('stylesheet_directory'); ?>/requests/';
		var count = <?php echo $i; ?>;
	</script>
	<?php
}
/**END GENERAL OPTIONS**/


function bs_general_options()
{
	global $pagenow;
	?>
	<div class="wrap">
		<h2>General Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			?>
			<div class="updated"><p>General options successfully updated!</p></div>
			<?php
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php admin_url( 'themes.php?page=bs-general-options' ); ?>">
				<?php 
				wp_nonce_field( 'update-options' ); 
				settings_fields( 'bs-general-options' );
				
				if ( $pagenow == 'themes.php' && $_GET['page'] == 'bs-general-options' )
				{
					?>
					<table class="form-table">
						<?php
						bs_general_feeds_providers_list();
						?>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="bs-general-options-submit" value="Y" />
				</p>
			</form>
		</div>
	</div>
	<?php
}


/**START SAVE SETTINGS**/
function bs_save_general_settings()
{
	global $pagenow;
	
	$settings = get_option( 'bs-general-options' );
	
	if ( $pagenow == 'themes.php' && $_GET['page'] == 'bs-general-options' )
	{
		$settings = $_POST['bs-general-options'];
	}
	/*
	if ( ! current_user_can('unfiltered_html' ) )
	{
		if ( $settings['homepage-third-block-content'] )
			$settings['homepage-third-block-content'] = stripslashes( esc_textarea( wp_filter_post_kses($settings['homepage-third-block-content']) ) );
	}
	*/
	$updated = update_option( 'bs-general-options', $settings );
}
/**END SAVE SETTINGS**/


function bs_load_settings_page()
{
	global $pagenow;
		
	if ( isset($_POST['bs-general-options-submit']) && $_POST['bs-general-options-submit'] == 'Y' )
	{
		bs_save_general_settings();
		wp_redirect( admin_url('themes.php?page=bs-general-options&updated=true') );
		exit;
	}
}

function bs_register_theme_options_pages()
{
	$settings_general = add_theme_page( 'Boarding Sports General Options', 'Boarding Sports General Options', 'edit_theme_options', 'bs-general-options', 'bs_general_options' );
	add_action( 'load-'.$settings_general, 'bs_load_settings_page' );
}
add_action('admin_menu', 'bs_register_theme_options_pages');